package com.atguigu.auth.controller;

import com.atguigu.auth.service.SysRoleService;
import com.atguigu.common.result.Result;
import com.atguigu.model.system.SysRole;
import com.atguigu.vo.system.AssginRoleVo;
import com.atguigu.vo.system.SysRoleQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author CaiLu
 * @date 2023/3/6
 * @apiNote
 */
@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Autowired
    SysRoleService sysRoleService ;

    //实现查询所有角色 http://localhost:8800/admin/system/sysRole/findAll

    @ApiOperation("查询所有角色")
    @GetMapping("findAll")
    public Result findAll() {
        List<SysRole> roleList = sysRoleService.list();
        return Result.ok(roleList);
    }
    //获取已分配角色与全部角色，进行页面展示
    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId) {
        Map<String, Object> roleMap = sysRoleService.findRoleByAdminId(userId);
        return Result.ok(roleMap);
    }

    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }
    //分页查询 模糊查询
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("分页查询")
    @GetMapping("{page}/{limit}")
    public Result pageQueryRole(@PathVariable int page , @PathVariable int limit , SysRoleQueryVo sysRoleQueryVo){
        //创建分页对象
        Page<SysRole> pagemodel  =  new Page<>(page, limit);
        //创建条件查询器
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        String roleName = sysRoleQueryVo.getRoleName();
        //查询全部显示
        if(!StringUtils.isEmpty(roleName)){
            wrapper.like(SysRole::getRoleName,roleName);
        }
        IPage<SysRole> page1 = sysRoleService.page(pagemodel, wrapper);
        return Result.ok(page1);
    }
    //获取角色
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("获取角色")
    @PostMapping("get/{id}")
    public Result get(@PathVariable Long id){
        SysRole byId = sysRoleService.getById(id);
        return Result.ok(byId);
    }
    //添加角色操作
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("添加角色")
    @PostMapping("save")
    public Result save(@RequestBody @Validated SysRole sysRole){
        boolean save = sysRoleService.save(sysRole);
        if(save){
            return Result.ok();
        }else {
            return Result.fail();
        }

    }
    //修改角色
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "修改角色")
    @PutMapping("update")
    public Result update(@RequestBody SysRole sysRole) {
        boolean b = sysRoleService.updateById(sysRole);
        if(b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //删除角色
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "删除角色")
    @DeleteMapping("remove/{id}")
    public Result delete(@PathVariable Long id){
        boolean b = sysRoleService.removeById(id);
        if(b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
    //根据id删除
    //java中的list集合会转换成前端数组格式,对象转成json格式
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "批量删除")
    @DeleteMapping("batchRemove")
    public Result delete(@RequestBody List<Long> idList) {
        boolean b = sysRoleService.removeByIds(idList);
        if(b){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }
}
